#!/usr/bin/env python
# encoding: utf-8
"""
token 验证的中间件
"""
import sys
sys.path.append("..")
import json
import const
from utils import verify_token

async def TokenVerifyMiddleware(context):
    """ """
    payload = json.loads(context.request.body)
    flag,user_id = verify_token(payload["token"])
    if flag:
        context.request.userId = user_id
        return await context.next()
        
    return {
            "code":const.ERR_TOKEN_INVALID,
             "msg":"Invalid token"
           }